#include "Util/EventSystem.h"
#include <gtest/gtest.h>
#include <string>
#include <memory>

// ## 测试说明
// 这些测试涵盖了以下方面：

// 1. EventDispatcher测试 ：
   
//    - 添加和检查事件监听器
//    - 移除事件监听器
//    - 事件分发功能
//    - 多个监听器的情况
//    - 不同类型的事件分发
// 2. NoticeCenter测试 ：
   
//    - 添加监听器和发布事件
//    - 使用模板方法发布事件
//    - 移除监听器

// 测试用的自定义事件类
class TestEvent : public Event {
public:
    TestEvent(const std::string& data) : data(data) {}
    
    std::string getType() const override {
        return "TestEvent";
    }
    
    std::string getData() const {
        return data;
    }
    
private:
    std::string data;
};

// 另一个测试用的事件类
class AnotherTestEvent : public Event {
public:
    std::string getType() const override {
        return "AnotherTestEvent";
    }
};

// EventDispatcher测试
class EventDispatcherTest : public ::testing::Test {
protected:
    EventDispatcher dispatcher;
};

// 测试添加和检查事件监听器
TEST_F(EventDispatcherTest, AddAndCheckEventListener) {
    EXPECT_FALSE(dispatcher.hasEventListener("TestEvent"));
    
    dispatcher.addEventListener("TestEvent", [](const std::shared_ptr<Event>&) {});
    
    EXPECT_TRUE(dispatcher.hasEventListener("TestEvent"));
}

// 测试移除事件监听器
TEST_F(EventDispatcherTest, RemoveEventListener) {
    dispatcher.addEventListener("TestEvent", [](const std::shared_ptr<Event>&) {});
    EXPECT_TRUE(dispatcher.hasEventListener("TestEvent"));
    
    dispatcher.removeEventListener("TestEvent");
    EXPECT_FALSE(dispatcher.hasEventListener("TestEvent"));
}

// 测试事件分发
TEST_F(EventDispatcherTest, DispatchEvent) {
    bool eventReceived = false;
    std::string receivedData;
    
    dispatcher.addEventListener("TestEvent", [&eventReceived, &receivedData](const std::shared_ptr<Event>& event) {
        eventReceived = true;
        auto testEvent = std::dynamic_pointer_cast<TestEvent>(event);
        if (testEvent) {
            receivedData = testEvent->getData();
        }
    });
    
    auto event = std::make_shared<TestEvent>("TestData");
    dispatcher.dispatchEvent(event);
    
    EXPECT_TRUE(eventReceived);
    EXPECT_EQ(receivedData, "TestData");
}

// 测试多个监听器
TEST_F(EventDispatcherTest, MultipleListeners) {
    int listenerCount = 0;
    
    dispatcher.addEventListener("TestEvent", [&listenerCount](const std::shared_ptr<Event>&) {
        listenerCount++;
    });
    
    dispatcher.addEventListener("TestEvent", [&listenerCount](const std::shared_ptr<Event>&) {
        listenerCount++;
    });
    
    auto event = std::make_shared<TestEvent>("TestData");
    dispatcher.dispatchEvent(event);
    
    EXPECT_EQ(listenerCount, 2);
}

// 测试不同类型的事件
TEST_F(EventDispatcherTest, DifferentEventTypes) {
    bool testEventReceived = false;
    bool anotherEventReceived = false;
    
    dispatcher.addEventListener("TestEvent", [&testEventReceived](const std::shared_ptr<Event>&) {
        testEventReceived = true;
    });
    
    dispatcher.addEventListener("AnotherTestEvent", [&anotherEventReceived](const std::shared_ptr<Event>&) {
        anotherEventReceived = true;
    });
    
    auto event1 = std::make_shared<TestEvent>("TestData");
    dispatcher.dispatchEvent(event1);
    EXPECT_TRUE(testEventReceived);
    EXPECT_FALSE(anotherEventReceived);
    
    auto event2 = std::make_shared<AnotherTestEvent>();
    dispatcher.dispatchEvent(event2);
    EXPECT_TRUE(anotherEventReceived);
}

// NoticeCenter测试
class NoticeCenterTest : public ::testing::Test {
protected:
    void SetUp() override {
        // 确保每个测试开始前移除所有监听器
        auto& center = NoticeCenter::getInstance();
        center.removeListener("TestEvent");
        center.removeListener("AnotherTestEvent");
    }
};

// 测试添加监听器和发布事件
TEST_F(NoticeCenterTest, AddListenerAndPostEvent) {
    auto& center = NoticeCenter::getInstance();
    
    bool eventReceived = false;
    std::string receivedData;
    
    center.addListener("TestEvent", [&eventReceived, &receivedData](const std::shared_ptr<Event>& event) {
        eventReceived = true;
        auto testEvent = std::dynamic_pointer_cast<TestEvent>(event);
        if (testEvent) {
            receivedData = testEvent->getData();
        }
    });
    
    auto event = std::make_shared<TestEvent>("NoticeData");
    center.postEvent(event);
    
    EXPECT_TRUE(eventReceived);
    EXPECT_EQ(receivedData, "NoticeData");
}

// 测试模板方法发布事件
TEST_F(NoticeCenterTest, PostEventTemplate) {
    auto& center = NoticeCenter::getInstance();
    
    bool eventReceived = false;
    
    center.addListener("AnotherTestEvent", [&eventReceived](const std::shared_ptr<Event>&) {
        eventReceived = true;
    });
    
    center.postEvent<AnotherTestEvent>();
    
    EXPECT_TRUE(eventReceived);
}

// 测试移除监听器
TEST_F(NoticeCenterTest, RemoveListener) {
    auto& center = NoticeCenter::getInstance();
    
    bool eventReceived = false;
    
    center.addListener("TestEvent", [&eventReceived](const std::shared_ptr<Event>&) {
        eventReceived = true;
    });
    
    center.removeListener("TestEvent");
    
    auto event = std::make_shared<TestEvent>("TestData");
    center.postEvent(event);
    
    EXPECT_FALSE(eventReceived);
}

// 主函数
int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}